package com.gitee.wsl.ext.okio.base

/**
 * Common interface for [CallState.Uploading] and [CallState.Downloading].
 */
interface TransferProgress {
    /**
     * The number of bytes that have been sent / received so far.
     */
    val bytesTransferred: Long

    /**
     * The number of bytes of the entire content. If it is unknown, this value is `-1`.
     */
    val bytesTotal: Long
}

/**
 * Percentage of transferred bytes to the entire content. (`0..100`)
 * If [TransferProgress.bytesTotal] is unknown, this value is `null`.
 */
val TransferProgress.progressPercentage: Int?
    get() {
        val total = bytesTotal
        return when {
            total <= 0 -> null
            total <= Long.MAX_VALUE / 100 -> (bytesTransferred * 100 / total).toInt()
            else -> (bytesTransferred / (total / 100)).toInt() // Avoid overflow.
        }
    }
